package com.example.sourcesprng.define_factorybean.student;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class StudentFactoryBean implements FactoryBean<Student> {
    @Override
    public Student getObject() throws Exception {
        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String name = method.getName();
                if ("say".equals(name)) {
                    System.out.println("调用代理的方法");
                }
                return "只要能拿到接口，就能在代理中实现任何功能";
            }
        };
        return (Student) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{Student.class}, handler);
    }

    @Override
    public Class<?> getObjectType() {
        return Student.class;
    }

}
